<?php
include_once('config/config.php');
$DepData = dbAll("SELECT * FROM `department`");

if ($_POST) {

  //封装数组
  $data = [
    'name' => $_POST['name'],
    'depid' => $_POST['depid']
  ];
  $action = $_POST['action'];
  if ($action == 'checkName') {
    $name = $_POST['name'];
    if (empty(trim($name))) {
      error('职位不能为空');
    }
    $jobId = dbFind("SELECT `id` FROM `job` WHERE `name` = '$name'");
    if ($jobId) {
      error('部门已经存在');
    } else {
      success('部门可以使用');
    }
  }

  //新增新数据到数据库
  $res = dbAdd('job', $data);
  if ($res) {
    ShowMsg('新增成功', 'jobList.php');
    exit;
  } else {
    ShowMsg('新增失败');
    exit;
  }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>职位</title>
  <!-- 引用公共头部 -->
  <?php include_once('common/meta.php'); ?>

  <style>
    .icon-list {
      margin-right: 5px;
    }
  </style>
</head>

<body>

  <!-- 引用公共头部 -->
  <?php include_once('common/header.php'); ?>

  <!-- 引用侧边菜单 -->
  <?php include_once('common/sidebar.php'); ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">新增职位</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="index.php">首页</a> <span class="divider">/</span></li>
      <li class="active">职位</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">

        <div class="btn-toolbar">
          <button class="btn btn-primary " onClick="location='jobList.php'"><i class="icon-list"></i>职位</button>
          <div class="btn-group">
          </div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="POST" enctype="multipart/form-data">
                <label>职位名称</label>
                <input type="text" value="" class="input-xxlarge" name="name" placeholder="请输入职位名称" required>
                <span id="name" style="margin-left: 10px;"></span>

                <label>部门</label>
                <select name="depid">
                  <?php foreach ($DepData as $item) { ?>
                    <option class="optId" value="<?php echo $item['id']; ?>"><?php echo $item['name']; ?></option>
                  <?php } ?>
                </select>
                <input style="margin-left: 10px;margin-top:-8px" class="btn btn-primary btn-submit" type="submit" value="提交" />
              </form>
            </div>
          </div>
        </div>

        <!-- 引用公共底部 -->
        <?php include_once('common/footer.php'); ?>

      </div>
    </div>
  </div>
</body>

</html>
<!-- 引用script -->
<?php include_once('common/script.php'); ?>

<script>
  $('.input-xxlarge').change(function() {
    let name = $('.input-xxlarge').val();

    $.ajax({
      type: 'post',
      url: 'jobAdd.php',
      data: {
        name,
        action: 'checkName'
      },
      dataType: 'json',
      success: function(res) {
        if (res.code === 1) {
          $('#name').html(res.msg).css({
            "color": "green"
          })
        } else {
          $('#name').html(res.msg).css({
            "color": "red"
          })
        }
      },
      // 请求失败的回调函数 404 301 500等
      error: function(error) {
        console.log(error)
      }
    })
  });
</script>